import service from './service'

import config from './config'
import { useStorage } from '@/hooks/web/useStorage'
import { useAppStoreWithOut } from '@/store/modules/app'
const { defaultHeaders } = config
const { getStorage } = useStorage()
const appStore = useAppStoreWithOut()
const request = (option: AxiosConfig) => {
  const userInfo = getStorage(appStore.getUserInfo)
  const { url, method, params, data, headersType, responseType } = option
  return service.request({
    url: url,
    method,
    params,
    data,
    responseType: responseType,
    headers: {
      'Content-Type': headersType || defaultHeaders,
      "Authorization":`Bearer ${userInfo ? userInfo.access_token : ""}`,
      "clientid":userInfo ? userInfo.client_id : "e5cd7e4891bf95d1d19206ce24a7b32e"
    }
  })
}

export default {
  get: <T = any>(option: AxiosConfig) => {
    return request({ method: 'get', ...option }) as Promise<IResponse<T>>
  },
  post: <T = any>(option: AxiosConfig) => {
    return request({ method: 'post', ...option }) as Promise<IResponse<T>>
  },
  delete: <T = any>(option: AxiosConfig) => {
    return request({ method: 'delete', ...option }) as Promise<IResponse<T>>
  },
  put: <T = any>(option: AxiosConfig) => {
    return request({ method: 'put', ...option }) as Promise<IResponse<T>>
  },
  cancelRequest: (url: string | string[]) => {
    return service.cancelRequest(url)
  },
  cancelAllRequest: () => {
    return service.cancelAllRequest()
  }
}
